package com.pet.shop.controller.mall;

import com.pet.config.Constants;
import com.pet.shop.entity.product.Product;
import com.pet.shop.entity.product.ProductPic;
import com.pet.shop.entity.product.ProductType;
import com.pet.shop.entity.shop.AdvertInfo;
import com.pet.shop.service.product.IProductPicService;
import com.pet.shop.service.product.IProductService;
import com.pet.shop.service.product.IProductTypeService;
import com.pet.shop.service.shop.IAdvertInfoService;
import com.pet.shop.utils.ApiResultUtil;
import com.pet.shop.vo.MallIndexVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/mall")
public class MallApiController {

    protected static final Logger logger = LoggerFactory.getLogger(MallApiController.class);

    @Autowired
    private IAdvertInfoService advertInfoService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IProductTypeService productTypeService;

    @Autowired
    private IProductPicService productPicService;

    @Value("${fileViewServer}")
    private String fileViewServer;
    @Value("${fileDefaultAdvert}")
    private String fileDefaultAdvert;
    @Value("${fileDefaultPic}")
    private String fileDefaultPic;

    /**
     * 商城首页
     * @param request
     * @return
     */
    @RequestMapping("mallIndex")
    public Map<String, Object> mallIndex(HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        List<Integer> typelist = new ArrayList<>();
        typelist.add(Constants.MALL_TYPE);
        typelist.add(Constants.MALL_RECOMMEND);
        map.put("typeList",typelist);
        List<AdvertInfo> resultList = advertInfoService.queryAdvertInfo(map);
        if(resultList == null || resultList.isEmpty()){
            return ApiResultUtil.error_param("banner图片不能为空");
        }
        resultList.forEach(advert -> {
            if(StringUtils.isEmpty(advert.getPicUrl())){
                advert.setPicUrl(fileViewServer + fileDefaultAdvert);
            }else if(!advert.getPicUrl().startsWith("http")){
                advert.setPicUrl(fileViewServer + advert.getPicUrl());
            }
        });
        map.clear();
        List<ProductType> productTypeList = productTypeService.queryProductTypeList(map);
        if(null == productTypeList || productTypeList.isEmpty()){
            return ApiResultUtil.error_param("产品类型不能为空");
        }

        MallIndexVo mallIndexVo = new MallIndexVo();
        mallIndexVo.setAdvertInfoList(resultList);
        mallIndexVo.setProductTypeList(productTypeList);
        return ApiResultUtil.success(mallIndexVo);
    }


    /**
     * 根据产品类型展现产品列表
     * @param request
     * @param type
     * @param priceOrderBy
     * @param dateOrderBy
     * @return
     */
    @RequestMapping("productTypeInfo")
    public Map<String, Object> productTypeInfo(HttpServletRequest request,String name,Integer type,String clickNum,String priceOrderBy,String dateOrderBy){
        if(null == type && null == priceOrderBy && null == dateOrderBy && null == clickNum){
            return ApiResultUtil.error_param("系统错误，请重试！");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("typeId",type);
        map.put("priceOrderBy",priceOrderBy);
        map.put("dateOrderBy",dateOrderBy);
        map.put("name",name);
        map.put("clickNum",clickNum);
        map.put("status", 1);
        List<Product> resultList = productService.queryProductInfoList(map);
        if(resultList == null) resultList = new ArrayList<>();
        resultList.forEach(product -> {
            if(StringUtils.isEmpty(product.getPicUrl())){
                product.setPicUrl(fileViewServer + fileDefaultPic);
            }else if(!product.getPicUrl().startsWith("http")){
                product.setPicUrl(fileViewServer + product.getPicUrl());
            }
        });

        return ApiResultUtil.success(resultList);
    }

    /**
     * 根据产品id展现产品详情
     * @param request
     * @param productId
     * @return
     */
    @RequestMapping("productInfo")
    public Map<String, Object> productInfo(HttpServletRequest request,Long productId){
        if(null == productId){
            return ApiResultUtil.error_param("系统错误，请重试！");
        }
        Product product = productService.getEntity(productId);
        if(null == product){
            return ApiResultUtil.error_param("系统错误，请重试！");
        }
        product.setViewTimes(product.getViewTimes() + 1);
        productService.updateEntity(product);
        if(StringUtils.isEmpty(product.getPicUrl())){
            product.setPicUrl(fileViewServer + fileDefaultPic);
        }else if(!product.getPicUrl().startsWith("http")){
            product.setPicUrl(fileViewServer + product.getPicUrl());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("productId",product.getId());
        List<ProductPic> productPicList = productPicService.queryProductPicByParams(map);
        //获取产品轮换图片
        product.setProductPicList(productPicList);
        /*//获取产品属性
        product.setPropertyList(productService.queryProductPropertyList(product.getId()));
        //获取产品规格
        product.setNormList(productService.queryProductNormList(product.getId()));*/
        return ApiResultUtil.success(product);
    }

    /**
     * 让clickNum加1
     * @param request
     * @param productId
     * @return
     */
    @RequestMapping("addClickNum")
    public Map<String, Object> addClickNum(HttpServletRequest request,Long productId){
        Product product = productService.getEntity(productId);
        product.setViewTimes(product.getViewTimes() + 1);
        productService.updateEntity(product);
        return ApiResultUtil.success(product);
    }



}
